package com.qf.java2106.test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.java2106.mapper.IAccountMapper;
import com.qf.java2106.mapper.IPersonMapper;
import com.qf.java2106.pojo.Account;
import com.qf.java2106.pojo.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


/**
 * @author q
 * @version V1.0
 * @date 2021/11/03
 **/
public class MybatisTest1103 {

    SqlSession sqlSession = null;
    IPersonMapper perMapper = null;
    IAccountMapper accountMapper = null;


    /**
     * 测试PageHelper
     * @throws Exception
     */
    @Test
    public void testPageHelper() throws Exception {
        PageHelper.startPage(4,3);
        //====中间不要写====
        List<Account> pageAccount = accountMapper.findAllAccount();
        for (Account account : pageAccount) {
            System.out.println(account);
        }
        PageInfo<Account> pageInfo = new PageInfo<>(pageAccount);
        System.out.println("页码:" + pageInfo.getPageNum());
        System.out.println("总条数:" + pageInfo.getTotal());
        System.out.println("总页数:" + pageInfo.getPages());
        System.out.println("每页显示条数:" + pageInfo.getPageSize());
        System.out.println("实际显示条数:" + pageInfo.getSize());
        System.out.println("是否是第一页:" + pageInfo.isIsFirstPage());
        System.out.println("是否是最后一页:" + pageInfo.isIsLastPage());
        System.out.println("是否有上一页:" + pageInfo.isHasPreviousPage());
        System.out.println("是否有下一页:" + pageInfo.isHasNextPage());
        System.out.println("当前页数据:" + pageInfo.getList());
    }

    /**
     * 主键非自增返回测试
     * @throws Exception
     */
    @Test
    public void testInsertAccountReturn() throws Exception {
        Account account = new Account();
        //account.setAccNum("1012");
        account.setPerName("提莫");
        account.setAccBalance(700d);
        System.out.println("插入前-->" + account.getAccNum());
        int i = accountMapper.insertAccountReturn(account);
        System.out.println("插入后-->" + account.getAccNum());
    }

    /**
     * 主键自增返回测试
     * @throws Exception
     */
    @Test
    public void testInsertAccount() throws Exception {
        Account account = new Account();
        account.setAccNum("1011");
        account.setPerName("提莫");
        account.setAccBalance(3000d);
        System.out.println("插入前-->" + account.getAccId());
        int i = accountMapper.insertAccount(account);
        System.out.println("插入后-->" + account.getAccId());
    }

    @Test
    public void testFindPersonWithAccountById() throws Exception {
        Person per = perMapper.findPersonWithAccountById(2);
        System.out.println(per);
        System.out.println(per.getAccounts());
    }

    @Test
    public void testFindAllPersonWithAccount() throws Exception {
        List<Person> allPersonWithAccount = perMapper.findAllPersonWithAccount();
        for (Person person : allPersonWithAccount) {
            System.out.println(person);
        }
    }

    @Before
    public void init() throws Exception {
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        sqlSession = factory.openSession();
        perMapper = sqlSession.getMapper(IPersonMapper.class);
        accountMapper = sqlSession.getMapper(IAccountMapper.class);
    }

    @After
    public void destroy() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }

}
